package com.kissoft.mgr.common;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.web.servlet.ModelAndView;

import com.kissoft.common.util.KisLogger;
import com.kissoft.common.util.KisLogger.LoggerCode;
import com.kissoft.common.util.KisUtil;

public class AuthAspect {
	
	private KisUtil util = KisUtil.getInstance();
	private KisLogger logger = KisLogger.getInstance();
	
	private HttpServletRequest request;
	
	public Object auth(ProceedingJoinPoint joinPoint) throws Throwable {
		
		logger.logWrite(LoggerCode.INFO, "인증 시작");
		
		String signatureString = joinPoint.getSignature().toShortString();
		ModelAndView mav = null;
		Object result = null;
		
		if(signatureString.indexOf("login") > 0){
			logger.logWrite(LoggerCode.INFO, "인증 예외 종료");
			return joinPoint.proceed();
		}
		
		String sessionId = util.getCookie("sessionId", request.getCookies());
		String email = util.getCookie("email", request.getCookies());
		
		logger.logWrite(LoggerCode.INFO, "sessionId = " + sessionId);
		logger.logWrite(LoggerCode.INFO, "email = " + email);

		if(sessionId != "" && email !=""){
			String loginView = "/mgr/login/login";
			mav = new ModelAndView();
			mav.setViewName(loginView);
			result = mav;
		}else{
			result = joinPoint.proceed();
		}
		
		logger.logWrite(LoggerCode.INFO, "인증 종료");
		
		return result;
	}
}
